/**
 * Copyright (c) 2003 held jointly by the individual authors.            
 *                                                                          
 * This library is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU Lesser General Public License as published      
 * by the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.                                            
 *                                                                            
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; with out even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details.                                                  
 *                                                                           
 * You should have received a copy of the GNU Lesser General Public License   
 * along with this library;  if not, write to the Free Software Foundation,   
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.              
 *                                                                            
 * > http://www.gnu.org/copyleft/lesser.html                                  
 * > http://www.opensource.org/licenses/lgpl-license.php
 */

package cn.duhengkui.national.valuelist.core.data.util;

import java.util.Collections;
import java.util.Map;


/**   
 * <table border="1">
 *	<tr><td>包名称：</td><td>cn.duhengkui.national.valuelist.core.data.util</td></tr>
 *	<tr><td>类名称：</td><td>TokenReplaceTextManipulator</td></tr>
 *	<tr><td>类描述：</td><td>sql处理固定条件（后于FilterTextManipulator）</td></tr>
 *	<tr><td>创建人：</td><td>杜恒奎</td></tr>
 *  <tr><td>创建时间：</td><td>2018年6月7日/下午3:00:36</td></tr> 
 *  <tr><td>版本：</td><td>1.0.0</td></tr>
 * </table>   
 *     
 */
public class TokenReplaceTextManipulator implements TextManipulator {
	

	private  String	startToken	= "${";

	private  String	endToken		= "}";

	private  String	nullValue	= "";



	/* (non-Javadoc)
	 * @see cn.duhengkui.national.valuelist.core.data.util.TextManipulator#manipulate(java.lang.StringBuilder, java.util.Map)
	 */
	public StringBuilder manipulate(StringBuilder text, Map<String, Object> model) {
		if (model == null) {
			model = Collections.emptyMap();
		}
		// Replace any [key] with the value in the whereClause Map.
		for (int end = 0, start; ((start = text.toString().indexOf(startToken, end)) >= 0);) {
			end = text.toString().indexOf(endToken, start);
			String key = text.substring(start + startToken.length(), end);
			Object modelValue = null;
			try {
				modelValue = ReflectUtil.getValue(model, key);
			} catch (Exception e) {} 
			if(text.substring(start-startToken.length(), start).equals("\\") && text.substring(end-endToken.length(), end).equals("\\")){
				text.replace(start-startToken.length(),start,"");
				text.replace(end-(endToken.length() + 1),end-endToken.length(),"");
			}else{
				text.replace(start, end + endToken.length(), (modelValue == null) ? nullValue : modelValue.toString());
				end -= (key.length() + startToken.length());
			}
		}
		return text;
	}

	/**
	 * @param endToken The endToken to set.
	 */
	public void setEndToken(String endToken) {
		this.endToken = endToken;
	}

	/**
	 * @param startToken The startToken to set.
	 */
	public void setStartToken(String startToken) {
		this.startToken = startToken;
	}

	/**
	 * @param nullValue The nullValue to set.
	 */
	public void setNullValue(String nullValue) {
		this.nullValue = nullValue;
	}
}